home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / cmplib_s.lha / cmplib_src / $convrhs1.P < prev    next >
Text File  |  1990-04-12  |  6KB  |  141 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* $convrhs1.P */
  25.  
  26. /* **********************************************************************
  27. $convrhs1_export([$convrhs/12]).
  28.  
  29. $convrhs1_use($meta,[_,$univ/2,$length/2]).
  30. $convrhs_use($aux1,[_,_,_,_,_,_,_,_,$logical_or/3]).
  31. $convrhs_use($computil1,[_,_,_,_,$max/3,_,_,_,_,$length1/2,
  32.                 $prefix_list/3,_,_,_,_,_,_,_,_,_]).
  33. $convrhs1_use($inline1,[$inline/2]).
  34. $convrhs1_use($blist,[_,_,$member1/2]).
  35. $convrhs1_use($getclauses,[_,_,$attach/2]).
  36. $convrhs1_use($targlist1,[_,$targlist/10,$targ_is/8]).
  37. ********************************************************************** */
  38.  
  39.  
  40. /*  The last 3 parameters of convrhs are as follows: NFlag is set to 1
  41.     if there is need for normalization in the clause, i.e. when execution
  42.     paths fork.  AFlag is set to 1 if it is clear that an "allocate" instr
  43.     must be generated, e.g. when a disjunction is present.  LFlag is 1 if
  44.     the subtree convrhs is processing contains a last literal of the clause,
  45.     0 if it does not (this is necessary to be able to handle variables
  46.     which occur only in the last literals of the clause, in different
  47.     disjunctive branches -- we have to be able to recognize them as temps.  */
  48.  
  49. $convrhs((Goal,Goals),and(Rgoal,_,Rgoals),VidTbl,
  50.         Pathin,Pathout,St,Litin,Litout,BindList,NFlag,AFlag,LFlag) :-
  51.     !,
  52.     $convrhs(Goal,Rgoal,VidTbl,
  53.         Pathin,Pathmid,St,Litin,Litmid,BindList,NFlag0,AFlag0,0),
  54.     $convrhs(Goals,Rgoals,VidTbl,
  55.         Pathmid,Pathout,St,Litmid,Litout,BindList,NFlag1,AFlag1,LFlag),
  56.     $logical_or(NFlag0,NFlag1,NFlag),
  57.     $logical_or(AFlag0,AFlag1,AFlag).
  58.  
  59. $convrhs(';'(Goal,Goals),or(Rgoal,_,Rgoals),VidTbl,
  60.             Pathin,Pathout,St,Litin,Litout,BindList,1,1,LFlag) :-
  61.     !,
  62.     $length1(BindList,BLen),
  63.     $convrhs(Goal,Rgoal,VidTbl,
  64.                 Pathin,Pathmid,St,Litin,Litout1,BindList,_,_,LFlag),
  65.     Pathmid1 is Pathmid + 1, $prefix_list(BindList,BLen,BindList1),
  66.     $convrhs(Goals,Rgoals,VidTbl,
  67.             Pathmid1,Pathout,St,Litin,Litout2,BindList1,_,_,LFlag),
  68.     $max(Litout1, Litout2, Litout).
  69.  
  70. $convrhs(if_then_else(Test,Goal1,Goal2),if_then_else(RTest,_,RGoal1,RGoal2),
  71.         VidTbl,Pin,Pout,St,Lin,Lout,BList,1,AFlag,LFlag) :-
  72.     !,
  73.     $convrhs_test(Test,RTest,VidTbl,Pin,St,Lin,Lmid0,BList),
  74.     $length1(BList,BLen),
  75.     $convrhs(Goal1,RGoal1,VidTbl,
  76.             Pin,Pmid1,St,Lmid0,Lout1,BList,_,AFlag1,LFlag),
  77.     $prefix_list(BList,BLen,BList1),
  78.     $convrhs(Goal2,RGoal2,VidTbl,
  79.             Pmid1,Pout,St,Lmid0,Lout2,BList1,_,AFlag2,LFlag),
  80.     $max(Lout1,Lout2,Lout),
  81.     $logical_or(AFlag1,AFlag2,AFlag).
  82. $convrhs(Goal,Rgoal,VidTbl,Pathno,Pathno,St,Litin,Litout,BindList,0,0,LFlag) :-
  83.     $convrhs_nextlitno(Goal,Litin,Litout),
  84.     $convgoal(Goal,Rgoal,Pathno,VidTbl,St,Litin,BindList,LFlag).
  85.  
  86. /*      "convrhs_test" converts the test for an if-then-else        */
  87.  
  88. $convrhs_test(','(T1,T2),and(RT1,_,RT2),VidTbl,Pathno,St,Lin,Lout,BList) :-
  89.     !,
  90.     $convrhs_test(T1,RT1,VidTbl,Pathno,St,Lin,Lmid0,BList),
  91.     $convrhs_test(T2,RT2,VidTbl,Pathno,St,Lmid0,Lout,BList).
  92. $convrhs_test(';'(T1,T2),or(RT1,_,RT2),VidTbl,Pathno,St,Lin,Lout,BList) :-
  93.     !,
  94.     $convrhs_test(T1,RT1,VidTbl,Pathno,St,Lin,Lout0,BList),
  95.     $convrhs_test(T2,RT2,VidTbl,Pathno,St,Lin,Lout1,BList),
  96.     $max(Lout0,Lout1,Lout).
  97. $convrhs_test(T,RT,VidTbl,Pathno,St,Lin,Lout,BList) :-
  98.     $convrhs_nextlitno(T,Lin,Lout),
  99.     $convgoal(T,RT,Pathno,VidTbl,St,Lin,BList,0).
  100.  
  101. $convrhs_nextlitno(Goal, Litnum, NextLitnum) :-
  102.     $univ(Goal,[Pred|Args]),
  103.     $length(Args, Arity),
  104.     (($inline(Pred, Arity), NextLitnum  is Litnum) ;
  105.      NextLitnum is Litnum + 1).    
  106.  
  107. $convgoal(Goal,TGoal,Pathno,VidTbl,St,Litno,BindList,LFlag) :-
  108.     St = st(Clist,Vlist),
  109.     $member1(nv(NVars),P), $member1(nv(NVars),P1),
  110.     $attach(c(Litno,Npars,P1),Clist),
  111.     (Goal = is(Lhs,Exp0) ->
  112.     (TGoal = '_call'(is,[A1,A2],P),
  113.      (var(Exp0) ->
  114.         Exp1 = Exp0+0 ;        /* to handle "X is Y " */
  115.         Exp1 = Exp0
  116.      ),
  117.      Npars = 2,
  118.      $member1(inline,P1),
  119.      $targ_is(Lhs,A1,VidTbl,Vlist,Pathno,Litno,1,LFlag),
  120.      $targ_is(Exp1,A2,VidTbl,Vlist,Pathno,Litno,2,LFlag)
  121.     ) ;
  122.     (functor(Goal,Pred,Npars),
  123.      ($inline(Pred,Npars) -> $member1(inline,P1) ; true),
  124.      $targlist(Goal,Npars,Args,VidTbl,Vlist,Pathno,Litno,1,UniList,BindList,LFlag),
  125.      N1 is Npars+1, 
  126.      $constr_goal(UniList,Pred,Args,P,N1,Pathno,Litno,NVars,Vlist,TGoal)
  127.     )
  128.     ).
  129.  
  130. $constr_goal([],Pred,Args,P,_,_,_,NVars,_,'_call'(Pred,Args,P)) :-
  131.     $member1(nv(NVars),P).
  132. $constr_goal([(V=Str)|UniRest],Pred,Args,P,N,Path,L,NVars,Vlist,
  133.             and('_call'(=, [V, Str], NV), _, TGoal1)) :-
  134.     V = v(Vid,Prag), Prag = vrec(_,_,_,_),
  135.     $member1(v(Vid,Occlist),Vlist),
  136.     gennum(Vno), $member1(o(Vno,Path,L,N,t,Prag),Occlist),
  137.     N1 is N+1, $member1(nv(NVars),NV),
  138.     $constr_goal(UniRest,Pred,Args,P,N1,Path,L,NVars,Vlist,TGoal1).
  139.  
  140. /* ---------------------------- $convrhs1.P --------------------------- */
  141.